package com.csi.bumblebee.service.impl;

import com.csi.bumblebee.domain.dictionary.JobType;
import com.csi.bumblebee.mapper.JobTypeMapper;
import com.csi.bumblebee.service.JobTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author HUOJT yeebs(查询全部岗位)
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class JobTypeServiceImpl implements JobTypeService {

    @Autowired
    @Qualifier("jdkRedisTemplate")
    private RedisTemplate<String, Serializable> redisTemplate;

    @Autowired
    private JobTypeMapper jobTypeMapper;

    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    @Override
    public JobType makePaper(String id) {

        // 从redis取
        JobType jobType = redisGet(id);

        // 从mysql取
        if (jobType == null) {
            jobType = jobTypeMapper.makePaper(id);

            // 写入redis
            if (jobType != null) {
                redisPut(jobType);
            }
        }

        return jobType;
    }



    /**
     * 查询所有三级岗位类型
     * @return List<JobType>
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public List<JobType> listThird() {

        List<JobType> jobTypeList = (List<JobType>) redisTemplate.opsForValue().get("jobTypeList");

        if (jobTypeList != null) {
            return jobTypeList;
        }else {
            jobTypeList = jobTypeMapper.listThird();
            redisTemplate.opsForValue().set("jobTypeList", (Serializable) jobTypeList);
            return jobTypeList;
        }
    }

    /**
     * 放入redis
     *
     * @param jobType 岗位类型
     */
    private void redisPut(JobType jobType) {
        redisTemplate.opsForValue().set(jobType.getId(), jobType, getExpireTime(), TimeUnit.SECONDS);
    }

    /**
     * 从redis取数据
     *
     * @param id 岗位类型id
     * @return 岗位类型
     */
    private JobType redisGet(String id) {
        redisTemplate.expire(id, getExpireTime(), TimeUnit.SECONDS);
        return (JobType) redisTemplate.opsForValue().get(id);
    }

    /**
     * 从redis取数据
     *
     * @param jobType 岗位类型
     * @return 岗位类型
     */
    private JobType redisGet(JobType jobType) {
        redisTemplate.expire(jobType.getId(), getExpireTime(), TimeUnit.SECONDS);
        return (JobType) redisTemplate.opsForValue().get(jobType.getId());
    }

    /**
     * 从redis删除
     *
     * @param id 岗位类型id
     * @return 删除的岗位类型
     */
    private JobType redisDelete(String id) {
        JobType jobType = redisGet(id);
        redisTemplate.delete(id);
        return jobType;
    }

    private long getExpireTime() {
        return 60 * 60 * 24 * 7;
    }


    /*
    查询所有的二级分类岗位
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public List<JobType> listJobTypeTwo() {
        return this.jobTypeMapper.listJobTypeTwo();
    }


    /**
     * 查询所有一级分类岗位
     * @return 一级岗位类型集合
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public List<JobType> listJobFather() {
        return this.jobTypeMapper.listJobFather();
    }

    /**
     * 查询所有岗位分类
     * @return 所有岗位集合
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public List<JobType>  listJobTypeTree(){

        List<JobType> listJobTree = (List<JobType>) redisTemplate.opsForValue().get("listJobTree");

        if (listJobTree == null || listJobTree.size() == 0 ){

             listJobTree = this.jobTypeMapper.listJobTypeTree();
             redisTemplate.opsForValue().set("listJobTree", (Serializable) listJobTree);
             return listJobTree;
        }
        return listJobTree;
    }



}
